import Util from "../../utils/util";
import {getCategoryDetail, getContentList} from "../../api/api";

const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    loading: false, // 是否加载中
    loadend: false, // 是否加载完毕
    loadTitle: '加载更多内容', // 提示语
    page: 1,
    lists: [],
    et_show: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({...options})

    const that = this
    wx.showLoading({
      title: '正在加载',
    })
    Util.chekWxLogin().then(res => {
      that.setData({
        userInfo: res.userInfo,
      });

      this.onLoadTabbar();
    }).then(() => {
      getCategoryDetail(that.data.id)
        .then((data) => {
          that.setData({
            category: data.data,
          })

          if (!Util.isEmpty(data.data.title)) {
            wx.setNavigationBarTitle({
              title: data.data.title,
            })
          }

        })

      that.setData({
        loadend: false,
        loading: false,
        list: [],
        page: 1,
      })
      that.getLists()

    }).catch(errmsg => {
      Util.Tips({title: errmsg}, {
        tab: 1, url: "/pages/index/index"
      })
    }).finally(() => {
      wx.hideLoading();
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },


  getLists() {
    const that = this
    let lists = that.data.lists || []

    if (that.data.loadend || that.data.loading) {
      return;
    }

    that.setData({loading: true, loadTitle: '加载更多内容'});
    let filter = that.data.filter || {}
    filter['category_id'] = this.data.id || 0;
    filter['page_num'] = that.data.page;

    getContentList(filter).then(res => {
      const list = res.data.items || []
      const loadend = res.data.last_page <= that.data.page;
      lists = Util.SplitArray(list, lists);

      that.setData({
        lists: lists,
        loadend: loadend,
        loading: false,
        loadTitle: loadend ? '已全部加载' : '正在加载...',
        total: res.data.total,
        page: that.data.page + 1,
        et_show: lists.length > 0,
      });

      wx.stopPullDownRefresh()
    }).catch(errmsg => {
      wx.stopPullDownRefresh()
      Util.Tips({title: errmsg})
    })
  },

  goToPage(e) {
    const that = this,
      key = e.currentTarget.dataset.key
    let lists = this.data.lists

    if (lists[key].mapping_status != 0)
      return;

    if (lists[key].shop_id) {
      wx.navigateTo({
        url: "../shop/index?sid=" + lists[key].shop_id
      })
    } else {
      wx.switchTab({
        url: "../index/index"
      })
    }
  },

  changeNav(e) {
    this.setData({
      loadend: false,
      loading: false,
      page: 1,
      lists: [],
      currentIndex: e.currentTarget.dataset.index
    })

    this.getLists()
  },

  onLoadTabbar: function () {//底部tabbar返回数据
    const that = this

    Util.checkIphone().then(res => {//检查是否是iphone x/11等
      that.setData({isIphoneX: res})
    })

  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

    this.setData({
      loadend: false,
      loading: false,
      page: 1,
      lists: [],
    });

    this.getLists();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.getLists();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})